<?php


namespace Cblood\Route\Http;


class HttpSession
{
    public function start()
    {
        session_start();
    }

    public function set($name, $value, $timeout = 1440)
    {
        $_SESSION[$name] = ['value' => $value, 'timeout' => time() + $timeout];
    }

    public function get($name)
    {
        if (!$this->isTimeout($name)) {
            return $_SESSION[$name]['value'];
        }
        return null;
    }

    /**
     * 判断session的某一个值是否过期
     * 过期了 true，没过期 false
     * @param $name
     * @return bool
     * @throws \Exception
     */
    public function isTimeout($name)
    {
        if (isset($_SESSION[$name])) {
            if ($_SESSION[$name]['timeout'] < time()) {
                unset($_SESSION[$name]);
                return true;
            }
            return false;
        }
        return true;
    }

    public function destroy()
    {
        session_destroy();
    }
}